package com.celeritymq.handler;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

import com.celeritymq.utils.CelerityBuffer;
import com.celeritymq.utils.CelerityTheadPool;

public class CelerityWriterImpl implements CelerityWriter {
	private CelerityTheadPool threadPool;
	
	CelerityWriterImpl(int poolSize) throws IOException {
		threadPool = new CelerityTheadPool(poolSize);
	}

	public void processWrite(SelectionKey key) {
		if (key.isWritable()) {
			SocketChannel sc = (SocketChannel) key.channel();
			CelerityBuffer buffer = (CelerityBuffer) key.attachment();
			if (buffer.getOutputBuffer().hasRemaining()) {
				try {
					sc.write(buffer.getOutputBuffer());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				// key.cancel();
			}
		}
	}

}
